<?php

namespace CbjCode\General\Library;

use CbjCode\General\Library\Interfaces\AuthInterface;
use Illuminate\Http\Request;

abstract class BaseAuth implements AuthInterface
{
    /**
     * 对象实例
     * @var object
     */
    protected static $instance;

    /**
     * 规则节点
     * @var array
     */
    protected $rules = [];

    /**
     * 构造请求对象
     * @var null
     */
    protected $request = null;

    /**
     * 错误提示语
     * @var string
     */
    protected $_error = "";

    /**
     * 模型对象
     * @var null
     */
    protected $model = null;

    /**
     * 初始化
     * BaseAuth constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 初始化
     * @access public static
     * @param Request $request
     * @return BaseAuth|object
     */
    public static function instance(Request $request)
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($request);
        }
        return self::$instance;
    }

    public function __get($name)
    {
        $auth = $this->request->user();
        return $auth[$name];
    }


    /**
     * 退出登陆
     * @return mixed|void
     */
    public function logout()
    {
        // TODO: Implement logout() method.
        $user = $this->request->user();
        return $user->currentAccessToken()->delete();
    }

    /**
     * 设置错误信息
     *
     * @param string $error 错误信息
     * @return BaseAuth
     */
    public function setError($error)
    {
        $this->_error = $error;
        return $this;
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->_error ? __($this->_error) : '';
    }



}
